package cn.itcast.bos.service.base.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.itcast.bos.dao.base.StandardRepository;
import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.base.StandardService;

@Service
@Transactional
public class StandardServiceImpl implements StandardService {

	private final Logger Log = LoggerFactory.getLogger(StandardServiceImpl.class);
	
	// 注入DAO
	@Autowired
	private StandardRepository standardRepository;

	@Override
	@CacheEvict(value = "standard", allEntries = true)
	public void save(Standard standard) {
		Log.info("StandardServiceImpl 保存收派标准...");
		standardRepository.save(standard);
	}

	@Override
    @Cacheable(value = "standard", key = "#pageable.pageNumber+'-'+#pageable.pageSize")
	public Page<Standard> findPageData(Pageable pageable) {
		Log.info("StandardServiceImpl findPageData 分页查询="+pageable.getPageNumber()+" "+pageable.getPageSize());
		return standardRepository.findAll(pageable);
	}

	@Override
	@Cacheable("standard")
	public List<Standard> findAll() {
		Log.info("StandardServiceImpl findAll 查找所有收派标准...");
		return standardRepository.findAll();
	}
	
	@Override
	@CachePut(value = "standard", key = "#pageable.pageNumber+'-'+#pageable.pageSize")
	public void updateKey(Pageable pageable) {
		Log.info("StandardServiceImpl updateKey 更新key，并执行方法="+pageable.getPageNumber()+" "+pageable.getPageSize());
	}
	
	@Override
	@CacheEvict(value = "standard", key = "#pageable.pageNumber+'-'+#pageable.pageSize")
	public void removeKey(Pageable pageable) {
		Log.info("StandardServiceImpl removeKey 清理指定key的缓存="+pageable.getPageNumber()+" "+pageable.getPageSize());
	}
	
	@Override
    @Cacheable(value = "standard")
	public List<Standard> testKey(Pageable pageable) {
		Log.info("测试一个参数默认的key，参数对象是Pageable");
		return standardRepository.findAll();
	}

	@Override
	@Cacheable(value = "standard")
	public List<Standard> testKey(String[] strs) {
		Log.info("测试一个参数默认的key，参数对象是String数组");
		return standardRepository.findAll();
	}

	@Override
	@Cacheable(value = "standard")
	public List<Standard> testKey(String[] strs, Pageable pageable) {
		// TODO Auto-generated method stub
		Log.info("测试多个参数默认的key，参数对象是String数组，Pageable对象");
		return standardRepository.findAll();
	}
}
